package com.EAIPClub.userauth.service.impl;

import com.EAIPClub.userauth.service.TokenService;
import com.api.API.UserAuthDataManager.UserAuthDataManagerAPI;
import com.api.API.UserDataManager.UserDataManagerAPI;
import com.api.ConstantPool;
import com.api.POJO.UserAuth.AuthReturnMsg;
import com.api.POJO.UserAuthDataManager.UserAuthDataManagerReturnMsg;
import com.api.POJO.UserDataManager.Result.UserDataReturnMsg;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description :用户Token登录业务逻辑实现
 * @Author :崔昌赫
 * @Date : 2021/10/23
 */

@Service
@Slf4j
public class TokenServiceImpl implements TokenService {
    private UserAuthDataManagerAPI userAuthDataManagerAPI;
    @Autowired
    public void setUserAuthDataManagerAPI(UserAuthDataManagerAPI userAuthDataManagerAPI) {
        this.userAuthDataManagerAPI = userAuthDataManagerAPI;
    }

    private UserDataManagerAPI userDataManagerAPI;
    @Autowired
    public void setUserDataManagerAPI(UserDataManagerAPI userDataManagerAPI) {
        this.userDataManagerAPI = userDataManagerAPI;
    }

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @SneakyThrows
    @Override
    public AuthReturnMsg token(String token) {
        log.info("即将通过token查询用户登录信息");
        AuthReturnMsg returnMsg = new AuthReturnMsg();
        List<String> Msg = new ArrayList<>();
        UserAuthDataManagerReturnMsg AuthReturnMsg = findUserAuthMsg(token);
        if (AuthReturnMsg==null){
            log.info("查询用户token失败!token:{}",token);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("查询用户token失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if (!AuthReturnMsg.getStatus().equals(ConstantPool.OK)){
            log.info("未查询到用户登录信息!token:{},返回信息:{}",token,AuthReturnMsg.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("未查询到用户登录信息!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("查询到用户登录信息为:{}",AuthReturnMsg);
        Date start = simpleDateFormat.parse(AuthReturnMsg.getAuthData().getDate());
        Date end = simpleDateFormat.parse(AuthReturnMsg.getAuthData().getExpiration());
        Date now = new Date();
        log.info("获取到时间为:start:{},end:{},now:{}",start,end,now);
        if(start.after(now)){
            log.info("不合法的时间判定!start:{},end:{},now:{}",start,end,now);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("不合法的时间!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(end.before(now)){
            log.info("该token已过期!现在时间:{},token时间:{}",now,end);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("该token已过期!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        String uid = AuthReturnMsg.getAuthData().getUid();
        UserDataReturnMsg userDataReturnMsg = findUserData(uid);
        log.info("获取到用户基本信息:{}",userDataReturnMsg);
        returnMsg.setEmail(userDataReturnMsg.getUser().getEmail());
        return returnMsg;
    }

    public UserAuthDataManagerReturnMsg findUserAuthMsg(String token){
        log.info("开始通过token查询用户登录信息.token:{}",token);
        return userAuthDataManagerAPI.getUserAuthDataByToken(token);
    }

    public UserDataReturnMsg findUserData(String uid){
        log.info("开始通过uid查询用户基本信息:token:{}",uid);
        return userDataManagerAPI.findUserBaseInfoByUid(uid);
    }
}
